package com.shujia.base;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/*
    从程序角度出发，它将hive看成了一个数据库去连接使用
    驱动包也就实现了java内置的sql接口，同样也实现了Driver接口

    所以这里的实现，符合我们之前说JDBC的步骤
    1、加载驱动
    2、创建数据库连接对象
    3、获取数据库操作对象
    4、执行sql语句
    5、若第四步是查询的，分析结果
    6、关闭资源
 */
public class JDBCAPi {
    public static void main(String[] args) throws Exception {
        //加载驱动
        Class.forName("org.apache.hive.jdbc.HiveDriver");

        Connection conn = DriverManager.getConnection("jdbc:hive2://master:10000/bigdata33");

        PreparedStatement preparedStatement = conn.prepareStatement("select * from students where clazz=?");

        preparedStatement.setString(1,"理科一班");

        ResultSet resultSet = preparedStatement.executeQuery();

        while (resultSet.next()){
            String id = resultSet.getString(1);
            String name = resultSet.getString(2);
            String age = resultSet.getString(3);
            String gender = resultSet.getString(4);
            String clazz = resultSet.getString(5);

            System.out.println(id+","+name+","+age+","+gender+","+clazz);
        }

        conn.close();

    }
}
